/**
 * 
 */
package com.android.dwt;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Log;

import com.android.dwt.service.DWTService;
import com.android.dwt.ui.TaskDetailAct;
import com.android.dwt.util.C;

/**
 * @author xiang
 *
 */
public class MyReceiver extends BroadcastReceiver {
	
	private final String ACTION_BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";
	private final String TAG = "MyReceiver";
	
	
	@Override  
    public void onReceive(Context context, Intent intent) {
        /*intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
        intent.setClass(context, TaskDetailAct.class);  
        context.startActivity(intent);  */
		
		String action = intent.getAction();
		
		if (action.equals(ACTION_BOOT_COMPLETED)) {		// restore alarms
//			DWTService.mIsRunning = true;
			
			Intent i = new Intent(context, DWTService.class);
			i.putExtra("boot_completed", true);
			context.startService(i);
			
			return;
		}
		
		if (action.equals(DWTService.ALARMS)) {			// handle alarms
			
			Log.e(TAG, "now handling alarms");
			
			setNotification(context, intent, DWTService.ALARMS);
			
			return;
		}
		
		if (action.equals(C.ACTION.LOCATION_ALERT)) {
			
			Log.e(TAG, "now location alert fires!");
			
			setNotification(context, intent, C.ACTION.LOCATION_ALERT);
			
			return;
		}
		
		if (action.equals(DWTService.GPS_ACTION)) {
			
			Log.e(TAG, "gps action goes here!");
			
			setNotification(context, intent, DWTService.GPS_ACTION);
		}
		
    }
	
	private void setNotification(Context context, Intent intent, String action) {
		
		String tickerText = "", contentTitle = "", contentText = "";
		NotificationManager nm;
		Intent itt = null;
		
		nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		
		if (action.equals(DWTService.ALARMS)) {
			
			long taskId = intent.getLongExtra("row_id", -1);
			TaskItem task = (TaskItem) intent.getSerializableExtra("task");
			
			tickerText = task.getTaskName();
			contentTitle = context.getString(R.string.rmd_expand_title);
			contentText = context.getString(R.string.rmd_expand_text) + " " + task.getTaskName();
			
			itt = new Intent(context, TaskDetailAct.class);
			
			itt.putExtra("row_id", taskId);
//			itt.putExtra("task", task);
			
			// Remove this alarm in sharedpreferences
			SharedPreferences sp = context.getSharedPreferences(DWTService.SP_ALARM, DWTService.MODE);
			Editor edit = sp.edit();
			
			int index = intent.getIntExtra("remind_index", 0);
			int count = sp.getInt("remind_count", 0);
			
			edit.remove("taskId["+index+"]");
			edit.remove("remind["+index+"]");
			edit.putInt("remind_count", count-1);
			
			edit.commit();
			
			Log.e(TAG, "notification finished and it is removed from sharepreferences");
			
		} else if (action.equals(DWTService.GPS_ACTION)) {
			
			tickerText = "无线网络和GPS定位已关闭";
			contentTitle = context.getString(R.string.app_name);
			contentText = "点此打开无线网络和GPS定位";
			
			itt = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
			
		} else if (action.equals(C.ACTION.LOCATION_ALERT)) {
			
			long taskId = intent.getLongExtra("row_id", -1);
			TaskItem task = (TaskItem) intent.getSerializableExtra("task");
			
			tickerText = task.getTaskName();
			contentTitle = context.getString(R.string.rmd_expand_title);
			contentText = "位置提醒：" + task.getTaskName();
			
			itt = new Intent(context, TaskDetailAct.class);
			itt.putExtra("row_id", taskId);
//			itt.putExtra("task", task);
			
		} else {
			
			;
		}
		
		Notification nfc = new Notification(
				R.drawable.clock,
				tickerText,
				System.currentTimeMillis());
		nfc.flags = Notification.FLAG_AUTO_CANCEL;
		
		PendingIntent nfcIntent = PendingIntent.getActivity(context, 0, itt, 0);
		nfc.setLatestEventInfo(context,	contentTitle, contentText, nfcIntent);
		
		// set ringtone and vibrate
//		notification.defaults = Notification.DEFAULT_ALL;
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		
		// Set ringtone
		String strRingtone = prefs.getString(context.getString(R.string.key_ringtone), "");
		Uri	uriRingtone = Uri.parse(strRingtone);
		if (uriRingtone != null) {
			
			nfc.sound = uriRingtone;
			
		} else {
			
			nfc.defaults |= Notification.DEFAULT_SOUND;
		}
		
		// Set vibrate
		Boolean vibrate = prefs.getBoolean(context.getString(R.string.key_vibrate), true);
		if (vibrate) {
			
			nfc.vibrate = new long[] {0, 1000, 700, 1000, 700, 1000};
		}
		
		nm.notify(C.TASK.TIME_NOTIFY, nfc);
	}
	
}
